﻿

using System;
using System.Globalization;
using System.ServiceProcess;
using System.Threading.Tasks;
using Quartz.Impl;

namespace Quartz.net
{


    public   class QuartzBase: ServiceBase
    {

        public static async Task CodeMethod()
        {
            Console.WriteLine(DateTime.Now.ToString(CultureInfo.InvariantCulture));
            //1.首先创建一个作业调度池
            var schedulerFactory = new StdSchedulerFactory();
            var scheduler = await schedulerFactory.GetScheduler();
            //2.创建出来一个具体的作业
            var job = JobBuilder.Create<SampleJob>().Build();
            //3.创建并配置一个触发器
            #region 每隔5秒执行一次Execute方法，无休止
            //ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create().WithSimpleSchedule(x => x.WithIntervalInSeconds(5).WithRepeatCount(int.MaxValue)).Build();
            #endregion

            #region 程序每5秒执行一次，一共执行50次，开始执行时间设定在当前时间，结束时间我设定在1小时后，不管50次有没有执行完，1小时候，程序都不再继续执行

            DateTimeOffset startTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddSeconds(1), 2);
            DateTimeOffset endTime = DateBuilder.NextGivenSecondDate(DateTime.Now.AddHours(1), 5);
            ISimpleTrigger trigger = (ISimpleTrigger)TriggerBuilder.Create().StartAt(startTime).EndAt(endTime)
                                      .WithSimpleSchedule(x => x.WithIntervalInSeconds(5).WithRepeatCount(50))
                                      .Build();
            #endregion

            #region  实现各种时间纬度的调用（使用cron-like）：在每小时的第10，20，30分钟，每分钟的第11，27秒执行一次
            //ICronTrigger trigger = (ICronTrigger)TriggerBuilder.Create().StartAt(startTime).EndAt(endTime)
            //                            .WithCronSchedule("11,27 10,20,30 * * * ? ")
            //                            .Build();
            #endregion

            #region testjoin
            var testJob = JobBuilder.Create<TestJob>().Build();
            ITrigger testTrigger = (ISimpleTrigger)TriggerBuilder.Create().StartNow().WithSimpleSchedule(x => x.WithIntervalInSeconds(1).RepeatForever()).Build();

            #endregion
            //4.加入作业调度池中
            await scheduler.ScheduleJob(job, trigger);
            await scheduler.ScheduleJob(testJob, testTrigger);

            //5.开始运行
            await scheduler.Start();
            Console.ReadKey();
        }
        public static async Task ConfigMethod()
        {
            var path = $"{AppDomain.CurrentDomain.BaseDirectory}log4net.xml";
            try
            {
                //工厂
                ISchedulerFactory factory = new StdSchedulerFactory();
                //启动
                IScheduler scheduler = await factory.GetScheduler();
                await scheduler.GetJobGroupNames();

                log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(path));
                await scheduler.Start();
                //Console.ReadKey();
            }
            catch (Exception ex)
            {
                throw;
            }

            //log4net.Config.XmlConfigurator.Configure(new System.IO.FileInfo(path));
            //SampleJob w = new SampleJob();
            //w.Execute(null);
        }

    }

    public class JobDemo : IJob
    {
        /// <summary>
        /// 这里是作业调度每次定时执行方法
        /// </summary>
        /// <param name="context"></param>
        public async Task Execute(IJobExecutionContext context)
        {
            Console.WriteLine(DateTime.Now.ToString());
        }
    }
}
